package net.neptech.node;

import com.alibaba.cloud.ai.graph.OverAllState;
import com.alibaba.cloud.ai.graph.action.NodeAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author HuangZhengmin
 * @desc  追加提问节点 - 当信息不足时生成补充问题
 * @Date 2025/8/19 17:36
 * @Version 1.0
 */
@Component
public class AdditionalQuestionNode implements NodeAction{
  private static final Logger logger = LoggerFactory.getLogger(AdditionalQuestionNode.class);

  @Autowired
  private ChatClient chatClient;

  @Override
  public Map<String, Object> apply(OverAllState state) {
    logger.info("进入追加提问节点");
    Map<String, Object> result = new HashMap<>();

    // 获取当前收集的症状和疾病推断结果
    String symptoms = (String) state.value("standardized_symptoms").orElse("");
    String diagnosis = (String) state.value("disease_inference").orElse("");

    // 生成针对性的补充问题
    String prompt = String.format(
            "患者症状: %s\n初步推断: %s\n" +
                    "基于以上信息，为了更准确诊断，需要询问患者1-2个补充问题。" +
                    "问题应简洁明了，聚焦于鉴别诊断关键点。", symptoms, diagnosis);

    String question = chatClient.prompt()
            .user(prompt)
            .call()
            .content();

    // 保存问题到状态
    result.put("additional_question_text", question);
    result.put("need_additional_questions", true);

    logger.info("生成追加问题: {}", question);
    return result;
  }
}
